<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" draggable overflow>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-form-item label="客户名称" prop="customer_name">
                <el-input v-model="form.customer_name" @blur="pinyin" placeholder="请输入客户名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="别名" prop="help_remember">
                <el-input v-model="form.help_remember" placeholder="请输入别名" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="mobile">
                <el-input v-model="form.mobile" placeholder="请输入联系方式" clearable></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址" clearable></el-input>
            </el-form-item>
            <!-- <el-form-item label="地区" prop="area_id">
                <el-select v-model="form.area_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="期初余额(元)" prop="init_money">
                <el-input v-model="form.init_money" placeholder="期初余额(元)" type="number" @mousewheel.prevent clearable :readonly="mode != 'add'"></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增客户',
				edit: '编辑客户',
				show: '查看客户'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				help_remember: "",
				customer_code: "",
				customer_name: "",
				address: "",
				system_disable: 0,
				mobile: "",
				init_money: "",
			},
			//验证规则
			rules: {
				customer_name: [{ required: true, message: '请输入客户名称' }],
			},
            areaList: [],
		}
	},
	mounted() {
		this.getArea();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.basic.client[this.form.id ? 'edit' : 'add'].post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.basic.client.detail.get({ id: data.id });
			this.form.customer_code = res.data.customer_code
			this.form.help_remember = res.data.help_remember
			this.form.customer_name = res.data.customer_name
			this.form.address = res.data.address
			this.form.system_disable = res.data.system_disable
			this.form.mobile = res.data.mobile
			this.form.init_money = res.data.init_money
		},
        // 地区列表
		async getArea() {
			var res = await this.$API.basic.area.all.get();
			this.areaList = res.data
		},
	}
}
</script>

<style></style>
